<?php

    class Zend_View_Helper_Profile extends Zend_View_Helper_Partial
    {

        private $messageModel;
        private $commonModel;
        private $user;
        private $friendGroupsAndFriends;

        function init($user_id, $showOnlyOnce = FALSE)
        {


            //profile popup code

            $x = new Zend_Auth_Storage_Session('User_Auth');
            $y = $x->read();
            $this->user_id = $y->user_id;
            $this->customModel = $custom_model = new Application_Model_Custom();
            $friend_group_records = $this->getFriendGroupAndFriends($user_id, $showOnlyOnce);
            $commonModel = new Application_Model_Common();
            $friend_records = $commonModel->getAllRecordFromInnerJoin('friend_requests', 'users', 'users.user_id = friend_requests.user_sent_to', 'user_sent_from=' . $user_id . ' and user_active="1"');
            $new_friend_array = array();

            foreach ($friend_records as $f_key => $f_value)
            {
                $friend_id = $f_value["user_id"];
                if (in_array($friend_id, $new_friend_array))
                    unset($friend_records[$f_key]);
                else
                    $new_friend_array[] = $friend_id;
            }


            $myEvents = "";
            if (!empty($y))
                $myEvents = $commonModel->getAllRecordDetail(TABLE_EVENTS, "event_ended = '0' AND event_created_by = " . $x->read()->user_id, "event_id DESC");

            //prd($myEvents);

            $frndCreatedEvents = array();
            $friendRatings = array();
            $otherUserComments = array();
            $friendArr = array();


            foreach ($friend_records as $frndKey => $frndValue)
            {
                $tmp_friend_id = $frndValue['user_id'];
                $friendArr[] = $tmp_friend_id;
                $frndCreatedEvents[] = $this->getCreatedEvent($tmp_friend_id);
                $friendRatings[] = $this->getUserRating($tmp_friend_id);
                $otherUserComments[] = $this->getWhoRatedAndCommentsOnUser($tmp_friend_id);
            }

            //prd($friend_group_records);
            foreach ($friend_group_records as $fKey => $fValue)
            {
                $frndArr[$fKey] = $fValue;
                $frndArr[$fKey]['frndArr'] = $friendArr;
                $frndArr[$fKey]['isLoggedIn'] = !empty($y) ? 'yes' : 'no';
                $frndArr[$fKey]['friendGroupsAndFriends'] = $this->getFriendGroupAndFriends($user_id);
                $frndArr[$fKey]['user_id'] = $user_id;
                //$frndArr[$fKey]['friend_created_events'] = $frndCreatedEvents;
                //prd($this->getCreatedEvent($user_id,'group',$fValue['group_id']));
                $frndArr[$fKey]['friend_created_events'] = $this->getCreatedEvent($user_id, 'group', $fValue['group_id']);
                $frndArr[$fKey]['friend_ratings'] = $friendRatings;
                $frndArr[$fKey]['otherUserComments'] = $otherUserComments;
                $frndArr[$fKey]['myEvents'] = $myEvents;
            }

            $this->friendGroupsAndFriends = $frndArr;

            //prd($frndArr);
            echo $this->view->partialLoop('profilepopup.phtml', $this->friendGroupsAndFriends);
        }

        function getCreatedEvent($friend_id, $type = "other", $group_id = false)
        {
            $custom_model = new Application_Model_Custom();
            $createdArr = array();
            if ($type == "other")
            {
//                $records = $custom_model->getAllEventDetails("event_created_by = $friend_id AND event_ended = '0'", "event_id DESC");
                $records = $custom_model->getAllEventDetails("event_created_by = $friend_id", "event_id DESC");
            }
            else
            {
                $friend_group_records = $this->customModel->getFriendsAndGroups($friend_id);
                foreach ($friend_group_records as $fr_key => $fr_value)
                {
                    if ($fr_value['group_id'] == $group_id)
                        foreach ($fr_value["friends_records"] as $new_key => $new_value)
                        {
//                            $records = $custom_model->getAllEventDetails("event_created_by = " . $new_value['user_id'] . " AND event_ended = '0'", "event_id DESC");
                            $records = $custom_model->getAllEventDetails("event_created_by = " . $new_value['user_id'], "event_id DESC");
//                            $createdArr[$new_value['user_id']] = array_merge($createdArr, $records);
                            $createdArr[$new_value['user_id']] =  $records;
                        }
                }
//                return $createdArr;
                $records = $createdArr;
            }
            return $records;
        }

        function getFriendGroup($user_id, $friend_id)
        {
            $this->commonModel = new Application_Model_Common;
            $result = $this->commonModel->getAllRecordDetail(TABLE_FRIEND_REQUESTS, ' user_sent_from = "' . $user_id . '" and user_sent_to = "' . $friend_id . '" ');

            $groupArr = array();
            foreach ($result as $key => $value)
            {
                $groupArr[$key]['group_id'] = $value['group_id'];
            }
            return $groupArr;
        }

        function getGroupMembersCount($user_id, $group_id)
        {
            $this->commonModel = new Application_Model_Common;
            $result = $this->commonModel->getAdapter()->select()->from(TABLE_FRIEND_REQUESTS, 'count(*) as total_friends')
                            ->where("user_sent_from = '" . $user_id . "' and group_id = '" . $group_id . "' ")->query()->fetch();

            return $result['total_friends'];
        }

        function getFriendGroupAndFriends($user_id, $showOnlyOnce = false)
        {
            $friend_group_records = $this->customModel->getFriendsAndGroups($user_id);

            $new_test_array = array();
            foreach ($friend_group_records as $fr_key => $fr_value)
            {
                foreach ($fr_value["friends_records"] as $new_key => $new_value)
                {
                    $friendEventsCounts = $this->customModel->getUserEventsCounts($new_value["user_id"]);

                    //=============================//
                    if ($showOnlyOnce == TRUE)
                    {
                        if (!in_array($new_value["user_id"], $new_test_array))
                        {
                            $new_test_array[] = $new_value["user_id"];
                            foreach ($friendEventsCounts as $fe_key => $fe_value)
                            {
                                $friend_group_records[$fr_key]["friends_records"][$new_key][$fe_key] = $fe_value;
                            }
                        }
                        else
                        {
                            unset($friend_group_records[$fr_key]["friends_records"][$new_key]);
                        }
                    }
                    else
                    {
                        $new_test_array[] = $new_value["user_id"];
                        foreach ($friendEventsCounts as $fe_key => $fe_value)
                        {
                            $friend_group_records[$fr_key]["friends_records"][$new_key][$fe_key] = $fe_value;
                        }
                    }

                    //---------------------------//
                }
            }
            return $friend_group_records;
        }

        function getUserRating($user_id)
        {
            $model = new Application_Model_Common;
            $result = $model->getAdapter()->select()->from(array("ur" => TABLE_RATING), new Zend_Db_Expr("SUM(rating) as totalrates, COUNT(rating_id) as totalvotes"))->where("user_rate_from = $user_id")->query()->fetch();

            $total_score = $result["totalrates"];
            $total_votes = $result["totalvotes"];

            $result = @$total_score / $total_votes;
            if (empty($result))
                $result = "0";

            return $result;
        }

        function getWhoRatedAndCommentsOnUser($user_id)
        {
            $model = new Application_Model_Common;
            $records = $model->getAdapter()->select()
                    ->from(array("ur" => TABLE_RATING))
                    ->joinLeft(array("u" => TABLE_USERS), "u.user_id=ur.user_rate_from")
                    ->where("ur.user_rate_to = $user_id")
                    ->order("ur.rating_id DESC")
                    ->limit("3");
            $records = $records->query()->fetchAll();

            foreach ($records as $key => $value)
            {
                $whereCond = "user_rate_to = $user_id AND user_rate_from = " . $value["user_id"];
                $result = $model->getdata(TABLE_RATING, $whereCond);
                $records[$key]["stars_rated"] = $result["rating"];
            }
//            prd($records);
            return $records;
        }

    }

?>